package com.ccjq.jq167.jquerystudent.servlet;

import com.ccjq.jq167.com.ccjq.jq167.jquerystudent.domain.User;
import com.ccjq.jq167.com.ccjq.jq167.jquerystudent.service.UserService;
import com.ccjq.jq167.com.ccjq.jq167.jquerystudent.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@WebServlet(name = "JqueryStudentServlet")
public class JqueryStudentServlet extends HttpServlet {
    private JqueryStudentServlet shopServlet;

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        shopServlet = new JqueryStudentServlet();
        User user = new User();
        //type=add 添加  update 修改  del删除  fetch查询
        String action = request.getParameter("action");
        HttpSession session = request.getSession();
        if (action.equals("add")) {
            request.getRequestDispatcher("/WEB-INF/jsp/adduser.jsp").forward(request, response);
        } else if (action.equals("ins")) {
            System.out.println("你执行了添加操作");
            String uuid = UUID.randomUUID().toString().replaceAll("-", "");
            String name = request.getParameter("name");
            int age = Integer.parseInt(request.getParameter("age"));
            String sex = request.getParameter("sex");
            String address = request.getParameter("address");
            String school = request.getParameter("school");
            user.setId(uuid);
            user.setName(name);
            user.setAge(age);
            user.setAddress(address);
            user.setSchool(school);
            user.setSex(sex);
            UserService dao = new UserServiceImpl();
            boolean b = dao.addAll(user);
            if (b) {
                System.out.println("添加成功");
                request.getRequestDispatcher("/WEB-INF/jsp/index.jsp").forward(request, response);
            } else {
                System.out.println("添加失败");
            }
        } else if (action.equals("fetch")) {
            System.out.println("你执行了查找操作");
            UserService dao = new UserServiceImpl();
            List<User> list = dao.queryAll();
            if (list != null) {
                System.out.println("查找成功");
                request.setAttribute("list", list);
                request.getRequestDispatcher("/WEB-INF/jsp/index.jsp").forward(request, response);
            } else {
                System.out.println("查找失败");
            }
        } else if (action.equals("update")) {
            UserService dao = new UserServiceImpl();
            String uuid = request.getParameter("uuid");
            User user1 = dao.queryById(uuid);
            if (user1 != null) {
                System.out.println("查找成功");
                session.setAttribute("shop", user1);
                request.getRequestDispatcher("/WEB-INF/jsp/update.jsp").forward(request, response);
            } else {
                System.out.println("查找失败");
            }
        } else if (action.equals("update1")) {
            //获取编号
            String uuid = request.getParameter("uuid");
            String name = request.getParameter("name");
            int age = Integer.parseInt(request.getParameter("age"));
            String sex = request.getParameter("sex");
            String address = request.getParameter("address");
            String school = request.getParameter("school");
            user.setId(uuid);
            user.setName(name);
            user.setAge(age);
            user.setAddress(address);
            user.setSchool(school);
            user.setSex(sex);
            UserService dao = new UserServiceImpl();
            boolean b = dao.updateAll(user);
            if (b) {
                System.out.println("修改成功");
                List<User> list = dao.queryAll();
                if (list != null) {
                    System.out.println("查找成功");
                    request.setAttribute("list", list);
                    request.getRequestDispatcher("/WEB-INF/jsp/index.jsp").forward(request, response);
                } else {
                    System.out.println("查找失败");
                }
            } else {
                System.out.println("修改失败");
            }
        }
        else if (action.equals("del")) {
            String uuid = request.getParameter("uuid");
            System.out.println(uuid);
            UserService dao = new UserServiceImpl();
            boolean b = dao.deleteId(uuid);
            if (b) {
                System.out.println("删除成功");
                List<User> list = dao.queryAll();
                if (list != null) {
                    System.out.println("查找成功");
                    request.setAttribute("list", list);
                    request.getRequestDispatcher("/WEB-INF/jsp/index.jsp").forward(request, response);
                } else {
                    System.out.println("查找失败");
                }
            } else {
                System.out.println("删除失败");
            }
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
